package org.lychie.jexcel.demo;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.lychie.jexcel.WritableExcel;
import org.lychie.jexcel.demo.model.Employee;
import org.lychie.jexcel.model.Body;
import org.lychie.jexcel.model.ValueFormat;

/**
 * 写出对象到EXCEL文档
 * 
 * @date 2015-01-21
 * @author Lychie Fan
 */
public class WriteExcel {

	public static void main(String[] args) {
		// 创建一个可写的EXCEL对象
		WritableExcel excel = new WritableExcel(getData());
		// 设置POJO属性与EXCEL单元格的映射关系
		excel.setMapper("id", "编号");
		excel.setMapper("name", "姓名");
		excel.setMapper("hiredate", "入职日期");
		excel.setMapper("salary", "薪资");
		
		// 单元格值格式对象
		ValueFormat format = excel.getValueFormat();
		// 设置薪资显示格式为货币格式
		format.set("salary", ValueFormat.CURRENCY_FORMAT);
		// 主体对象
		Body body = excel.getBody();
		// 设置单元格内容水平方向居中
		body.setHorizontalAlignment(Body.HORIZONTAL_CENTER);
		
		// 将对象内容写出到EXCEL文档
		excel.write(new File("src/main/resources/employee.xlsx"));
	}

	private static List<Employee> getData() {
		List<Employee> list = new ArrayList<Employee>();
		list.add(new Employee("杨忠杰"));
		list.add(new Employee("叶水燕"));
		list.add(new Employee("杨晓婷"));
		list.add(new Employee("叶国珠"));
		list.add(new Employee("何国群"));
		return list;
	}

}